/*
 * Allocation.h
 *
 *  Created on: Mar 8, 2010
 *      Author: Vu Phuong Hoang
 */

#ifndef ALLOCATION_H_
#define ALLOCATION_H_

template<class T>
bool newMatrix(T**& m, const unsigned int rows, const unsigned int columns, const T defaultValue) {
	m = new T*[rows];

	if (m != NULL) {
		for (unsigned int i = rows; i > 0; --i) {
			m[i - 1] = new T[columns];
			if (m[i - 1] == NULL) {
				return false;
			}
			for (unsigned int j = columns; j > 0; --j) {
				m[i - 1][j - 1] = defaultValue;
			}
		}
		return true;
	} else {
		return false;
	}
}

template<class T>
T* copyArray(T* x, const unsigned int from, const unsigned int length) {
	if (length > 0) {
		T* a = new T[length];
		if (a == NULL) {
			return NULL;
		}
		length += from;
		for (int i = from; i < length; ++i) {
			a[i] = x[i];
		}
		return a;
	}
	return NULL;
}

#endif /* ALLOCATION_H_ */
